package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.UserGold;
import cn.wolfcode.luowowo.article.mapper.UserGoldMapper;
import cn.wolfcode.luowowo.article.service.IUserGoldService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserGoldServiceImpl implements IUserGoldService {

    @Autowired
    private UserGoldMapper userGoldMapper;

    @Override
    public void saveOrUpdate(UserGold userGold) {

        if(userGold.getId() == null){
            userGoldMapper.insert(userGold);
        }else{
            userGoldMapper.updateByPrimaryKey(userGold);
        }
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        userGoldMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UserGold get(Long id) {
        return userGoldMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<UserGold> listAll() {
        return userGoldMapper.selectAll();
    }

    @Override
    public UserGold selectByUserId(Long uid) {
        return userGoldMapper.selectByUserId(uid);
    }

    @Override
    public void addCoin(Long uid, int addCoin) {
        UserGold userGold = userGoldMapper.selectByPrimaryKey(uid);

        userGold.setCoin(userGold.getCoin() + addCoin);

        userGoldMapper.updateByPrimaryKey(userGold);

    }
}
